package cn.t.repository.impl.user;

import cn.t.converter.user.ResourceInfoConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.user.ResourceInfoDO;
import cn.t.persistence.entity.user.ResourceInfoPO;
import cn.t.persistence.mapper.user.ResourceInfoMapper;
import cn.t.repository.user.ResourceInfoRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源权限表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ResourceInfoRepositoryImpl extends RepositoryImpl<ResourceInfoMapper, ResourceInfoPO, ResourceInfoConverter, ResourceInfoDO> implements ResourceInfoRepository {


    @Override
    public List<ResourceInfoDO> findByModel(ResourceInfoDO resourceInfoDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ResourceInfoPO> list = getWrapper(resourceInfoDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }


    private LambdaQueryChainWrapper<ResourceInfoPO> getWrapper(ResourceInfoDO resourceInfoDO) {
        LambdaQueryChainWrapper<ResourceInfoPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(resourceInfoDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(resourceInfoDO.getGmtCreatedEnd())) {
            wrapper.between(ResourceInfoPO::getGmtCreated, resourceInfoDO.getGmtCreatedStart(), resourceInfoDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(resourceInfoDO.getGmtModifiedEnd())) {
            wrapper.between(ResourceInfoPO::getGmtModified, resourceInfoDO.getGmtModifiedStart(), resourceInfoDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getScenarioId())) {
            wrapper.eq(ResourceInfoPO::getScenarioId, resourceInfoDO.getScenarioId());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getResourceName())) {
            wrapper.eq(ResourceInfoPO::getResourceName, resourceInfoDO.getResourceName());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getParentId())) {
            wrapper.eq(ResourceInfoPO::getParentId, resourceInfoDO.getParentId());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getSort())) {
            wrapper.eq(ResourceInfoPO::getSort, resourceInfoDO.getSort());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getResourceType())) {
            wrapper.eq(ResourceInfoPO::getResourceType, resourceInfoDO.getResourceType());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getResourceUrl())) {
            wrapper.eq(ResourceInfoPO::getResourceUrl, resourceInfoDO.getResourceUrl());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getIconUrl())) {
            wrapper.eq(ResourceInfoPO::getIconUrl, resourceInfoDO.getIconUrl());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getIsHidden())) {
            wrapper.eq(ResourceInfoPO::getIsHidden, resourceInfoDO.getIsHidden());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getRemark())) {
            wrapper.eq(ResourceInfoPO::getRemark, resourceInfoDO.getRemark());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getStatus())) {
            wrapper.eq(ResourceInfoPO::getStatus, resourceInfoDO.getStatus());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getResourceCode())) {
            wrapper.eq(ResourceInfoPO::getResourceCode, resourceInfoDO.getResourceCode());
        }
        if (!ObjectUtils.isEmpty(resourceInfoDO.getResourceName())) {
            wrapper.eq(ResourceInfoPO::getResourceName, resourceInfoDO.getResourceName());
        }
        return wrapper;
    }

    @Override
    public long count(ResourceInfoDO resourceInfoDO) {
        return getWrapper(resourceInfoDO).count();
    }
}
